//
//  NetworkManager.swift
//  AutoModel
//
//  Created by 好米有易 on 2019/8/5.
//  Copyright © 2019 own. All rights reserved.
//

import Cocoa

enum RequstType: String {
    case GET = "GET"
    case POST = "POST"
}

class NetworkManager {

    static let shared = NetworkManager()

    private init(){}

    /// 请求
    ///
    /// - Parameters:
    ///   - type: 请求类型
    ///   - url: 请求连接字符串
    ///   - paraters: 请求参数
    ///   - callBack: 请求结果
    func request(type:RequstType, url: String, paraters:[String: Any]?, callBack:@escaping ((_ data: Data) -> ())) {
        guard let URL = URL(string: url) else {return}
        var request = URLRequest(url: URL , cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
        request.httpMethod = type.rawValue
        let session = URLSession.shared
        let dataTask = session.dataTask(with: request) { (data, response, error) in
            if error != nil {return}
            if data == nil {return}
            if response == nil {return}
            callBack(data!)
        }
        dataTask.resume()
    }
}
